package com.visikard.ui.fragment;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.visikard.application.UIMessage;
import com.visikard.ui.R;

public class BaseFragment extends Fragment implements OnClickListener {

	private static final String RETAINED_FRAGMENT_TAG = "retained_fragment";

	// Handler class
	static class UIHandler extends Handler {
		private WeakReference<BaseFragment> mReference;

		public UIHandler(BaseFragment reference) {
			mReference = new WeakReference<BaseFragment>(reference);
		}

		@Override
		public void handleMessage(Message msg) {
			BaseFragment ref = getReference();
			// Phuoc: Added getActivity check to ensure fragment fully attached
			if (ref != null && ref.getActivity() != null) {
				// message is handled by application
				if (ref.handleMessage(msg))
					return;
			}
			super.handleMessage(msg);
		}

		public BaseFragment getReference() {
			if (mReference != null)
				return mReference.get();
			return null;
		}
	}

	// Handler instance
	protected UIHandler mHandler;

	// UI base widgets
	protected Dialog mProgress;

	// Saved instance
	private RetainedFragment mRetainedFragment;

	@Override
	public void onCreate(Bundle arg0) {
		// Log.d("Phuoc", "BaseFragment----------------------------onCreate");
		super.onCreate(arg0);

		mHandler = new UIHandler(this);

		mRetainedFragment = (RetainedFragment) getFragmentManager().findFragmentByTag(RETAINED_FRAGMENT_TAG);
		if (mRetainedFragment == null) {
			mRetainedFragment = new RetainedFragment();
			getFragmentManager().beginTransaction().add(mRetainedFragment, RETAINED_FRAGMENT_TAG).commit();
		}
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		// Log.d("Phuoc", "BaseFragment----------------------------onAttach");
	}

	@Override
	public void onDetach() {
		// TODO Auto-generated method stub
		super.onDetach();
		// Log.d("Phuoc", "BaseFragment----------------------------onDetach");
	}

	// Handle message method
	protected boolean handleMessage(Message msg) {
		switch (msg.what) {
			case UIMessage.MSG_SHOW_DIALOG:
				showProgressDialog();
				return true;
			case UIMessage.MSG_DISMISS_DIALOG:
				dismissProgressDialog();
				return true;
			case UIMessage.MSG_NETWORK_ERROR:
				Toast.makeText(this.getActivity(), getString(R.string.network_problem), Toast.LENGTH_SHORT).show();
				return true;
			case UIMessage.MSG_UNKNOW_ERROR:
				Toast.makeText(this.getActivity(), getString(R.string.unknown_problem), Toast.LENGTH_SHORT).show();
				return true;
		}
		return false;
	}

	public void showProgressDialog() {
		if (mProgress == null) {
			mProgress = new Dialog(this.getActivity(), android.R.style.Theme_Translucent_NoTitleBar);
			mProgress.setContentView(R.layout.progressdialog);
			mProgress.setCancelable(false);
		}
		mProgress.show();
	}

	public void dismissProgressDialog() {
		if (mProgress != null && mProgress.isShowing()) {
			mProgress.dismiss();
			mProgress = null;
		}
	}

	protected final void putSavedObject(String key, Object obj) {
		mRetainedFragment.putObject(key, obj);
	}

	protected final Object getSavedObject(String key) {
		return mRetainedFragment.getObject(key);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
